package com.shop.cereshop.commons.tencent.moderation.util;

import com.shop.cereshop.commons.tencent.moderation.constant.ModerationConstant;
import org.apache.commons.lang3.StringUtils;

/**
 * @description: 腾讯内容审核工具类
 * @author zdd
 * @date 2025/10/26 17:00
 * @version 1.0
 */
public class ModerationUtils {

    /**
     * 获取审核建议的中文描述
     * @param suggestion 审核建议
     * @return 中文描述
     */
    public static String getSuggestionDesc(String suggestion) {
        if (StringUtils.isBlank(suggestion)) {
            return "未知";
        }
        switch (suggestion) {
            case ModerationConstant.SUGGESTION_BLOCK:
                return "违规";
            case ModerationConstant.SUGGESTION_REVIEW:
                return "疑似违规";
            case ModerationConstant.SUGGESTION_PASS:
                return "正常";
            default:
                return "未知";
        }
    }

    /**
     * 获取违规标签的中文描述
     * @param label 违规标签
     * @return 中文描述
     */
    public static String getLabelDesc(String label) {
        if (StringUtils.isBlank(label)) {
            return "未知";
        }
        switch (label) {
            case ModerationConstant.LABEL_NORMAL:
                return "正常";
            case ModerationConstant.LABEL_PORN:
                return "色情";
            case ModerationConstant.LABEL_SEXY:
                return "性感";
            case ModerationConstant.LABEL_POLITY:
                return "政治";
            case ModerationConstant.LABEL_ILLEGAL:
                return "违法";
            case ModerationConstant.LABEL_ABUSE:
                return "谩骂";
            case ModerationConstant.LABEL_TERROR:
                return "暴恐";
            case ModerationConstant.LABEL_AD:
                return "广告";
            default:
                return "其他";
        }
    }

    /**
     * 判断审核结果是否通过
     *
     * @param suggestion 审核结果
     * @return true: 通过，false: 未通过
     */
    public static boolean isPass(String suggestion) {
        return ModerationConstant.SUGGESTION_PASS.equals(suggestion);
    }

    /**
     * 判断审核结果是否需要人工审核
     *
     * @param suggestion 审核结果
     * @return true: 需要人工审核，false: 不需要人工审核
     */
    public static boolean needsReview(String suggestion) {
        return ModerationConstant.SUGGESTION_REVIEW.equals(suggestion);
    }

    /**
     * 判断审核结果是否违规
     *
     * @param suggestion 审核结果
     * @return true: 违规，false: 未违规
     */
    public static boolean isBlocked(String suggestion) {
        return ModerationConstant.SUGGESTION_BLOCK.equals(suggestion);
    }

    /**
     * 判断是否为高置信度结果
     * @return true: 高置信度(score >= 90), false: 中低置信度
     */
    public static boolean isHighConfidence(Long score) {
        return score != null && score >= 90;
    }

    /**
     * 判断是否为中置信度结果
     * @return true: 中置信度(60 <= score < 90), false: 高低置信度
     */
    public static boolean isMediumConfidence(Long score) {
        return score != null && score >= 60 && score < 90;
    }

    /**
     * 判断是否为低置信度结果
     * @return true: 低置信度(score < 60), false: 中高置信度
     */
    public static boolean isLowConfidence(Long score) {
        return score != null && score < 60;
    }

    /**
     * 计算风险级别
     * @param success 审核是否成功 (true: 成功，false: 失败)
     * @param suggestion 审核建议
     * @param score 置信度分数
     * @return 风险级别字符串 ("HIGH", "MEDIUM", "LOW", "UNKNOWN")
     */
    public static String getRiskLevel(boolean success, String suggestion, Long score) {
        if (!success) {
            return "UNKNOWN";
        }

        if (ModerationConstant.SUGGESTION_BLOCK.equals(suggestion)) {
            if (isHighConfidence(score)) return "HIGH";
            if (isMediumConfidence(score)) return "MEDIUM";
            return "LOW";
        } else if (ModerationConstant.SUGGESTION_REVIEW.equals(suggestion)) {
            if (isHighConfidence(score)) return "MEDIUM";
            return "LOW";
        } else {
            return "LOW";
        }
    }

}
